<?php

require_once __DIR__.'/../config.php';

class MySQL{
    private $link;             //数据库连接标识;
    static $_instance; //存储对象

    /**
     * 构造函数
     * 私有
     */
    private function __construct() {
        try {
            $this->$link = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASSWORD);
            $this->$link->exec('SET names utf8');
        } catch (PDOException $e) {
            $this->err($e->getMessage());
        }
    }

    /**
     * 防止被克隆
     *
     */
    private function __clone(){}

    public static function getInstance(){
        if(FALSE == (self::$_instance instanceof self)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 
     */
    public function prepare($sql)
    {
        return $this->$link->prepare($sql);
    }

    public function lastInsertId()
    {
        return $this->$link->lastInsertId();
    }

    /**
     * 错误信息输出
     */
    protected function err($strErrMsg) {
        print "MySQL Error!: " . $strErrMsg . "<br/>";
        exit();
    }
}